<template>
  <div class="login">
    <header class="header">
      <div class="logo">12306铁路购票@wtt</div>
      <div class="wrapper">
        <div class="nav-list">首页</div>
        <div class="nav-list">文档</div>
        <div class="nav-list">博客</div>
        <div class="nav-list">社区</div>
      </div>
    </header>
    <section class="content">
      <div class="white-panel">
        <h1 class="title">登录</h1>
        <el-form :model="form" :rules="rules">
          <el-form-item prop="usernameOrMailOrPhone">
            <i class="iconfont icon-user"></i>
            <el-input
              v-model="form.usernameOrMailOrPhone"
              placeholder="请输入用户名/邮箱/手机号"
            />
          </el-form-item>
          <el-form-item prop="password">
            <i class="iconfont icon-lock"></i>
            <el-input
              v-model="form.password"
              placeholder="请输入密码"
              show-password
            />
          </el-form-item>
        </el-form>
        <div class="form-btn">
          <a href="#">忘记密码？</a>
          <button class="btn" @click="handleLogin">登录</button>
        </div>
      </div>
    </section>
  </div>
</template>
<script setup>
import { useRouter } from "vue-router"
import { login } from "@/api/user"
import { setToken, setUserId, setUserName } from "@/utils/auth"
import { ref } from "vue"
const router = useRouter()
const form = ref({
  usernameOrMailOrPhone: "admin",
  password: "admin123456",
})

const rules = ref({
  usernameOrMailOrPhone: [
    { required: true, message: "请输入用户名", trigger: "blur" },
  ],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
})

const handleLogin = async () => {
  const resp = await login(
    form.value.usernameOrMailOrPhone,
    form.value.password
  )
  setToken(resp.data.accessToken)
  setUserId(resp.data.userId)
  setUserName(resp.data.username)
  if (resp.success) {
    router.push("/ticketSearch")
  }
}
</script>
<style scoped lang="scss">
.login {
  width: 100%;
  height: 100%;
  background: url(@/assets/loginBg.png);
  .content {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
    width: 40%;
    left: 20%;
    margin: auto;
    padding: 0 50px;
    min-width: 800px;
    height: 600px;
    background-color: rgba(30, 30, 30, 0.9);
    .white-panel {
      width: 400px;
      height: 600px;
      background-color: #fff;
      padding: 30px;
      display: flex;
      flex-direction: column;
      margin: auto;
      .title {
        font-size: 24px;
        font-weight: bolder;
        padding: 20px 0;
        color: #202020;
      }
      .form-btn {
        display: flex;
        justify-content: space-between;
        align-content: center;
        a {
          text-decoration: none;
          color: #1890ff;
          transition: color 0.3s;
          line-height: 32px;
        }
        a:hover {
          color: #40a9ff;
        }
        .btn {
          background-color: rgb(32, 32, 32);
          border: none;
          color: #fff;
          height: 32px;
          padding: 4px 15px;
          font-size: 14px;
          border-radius: 4px;
          cursor: pointer;
        }
      }
    }
    .el-form-item {
      display: flex;
      width: 340px;
      margin-bottom: 24px;
      background-color: #fff;
      padding: 6.5px 11px;
      border: 1px solid #d9d9d9;
      border-radius: 4px;
      transition: all 0.3s;
      .el-input {
        width: 292px;
        ::v-deep .el-input__wrapper {
          border: none;
          box-shadow: none;
          .el-input__inner {
            height: 25px;
            font-size: 16px;
            color: black;
          }
        }
      }
      ::v-deep .el-form-item__error {
        font-size: 13px;
        padding-top: 12px;
      }
    }
    .el-form-item:hover {
      border: 1px solid #03a4f4;
    }
  }
  .header {
    background-color: transparent;
    position: fixed;
    width: 100%;
    min-width: 800px;
    height: 64px;
    top: 0;
    z-index: 100;
    box-shadow: 0 2px 10px 0 rgba(0, 0, 0, 0.08);
    line-height: 64px;
    padding: 0 50px;
    .logo {
      font-size: 16px;
      font-weight: 600;
      color: #e8e8e8;
      float: left;
    }
    .wrapper {
      display: flex;
      justify-content: flex-end;
      .nav-list {
        margin: 0 30px;
        color: white;
        cursor: pointer;
        opacity: 0.8;
      }
    }
  }
}
</style>
